package com.kedll.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author QiQi
 */
public class LetterListView extends View {

    int choose = -1;
    boolean showBkg = false;
    ;
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    private String[] b = new String[0]; /*= {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
            "Y", "Z"}*/
    private Paint paint = new Paint();
    private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int textColor = Color.GRAY;

    {
        circlePaint.setColor(Color.RED);
        circlePaint.setStyle(Paint.Style.FILL);
    }

    public LetterListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public LetterListView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public LetterListView(Context context) {
        super(context);
    }

    /**
     * 设置快捷标签
     *
     * @param b
     */
    public void setLetterLabel(String[] b) {
        this.b = b;
        invalidate();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final float x = event.getX();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener =
                onTouchingLetterChangedListener;
        final int height = getHeight() - getPaddingTop() - getPaddingBottom();
        if (y < getPaddingTop()) {
            return true;
        }
        final int c = (int) (y / height * b.length);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                showBkg = true;
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < b.length) {
                        listener.onTouchingLetterChanged(b[c], y, x);
                        choose = c;
                        invalidate();
                    }
                }

                break;
            case MotionEvent.ACTION_MOVE:
                if (oldChoose != c && listener != null) {
                    if (c >= 0 && c < b.length) {
                        listener.onTouchingLetterChanged(b[c], y, x);
                        choose = c;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                showBkg = false;
//                choose = -1;
                listener.onTouchingLetterEnd();
                invalidate();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showBkg) {
//            canvas.drawColor(Color.parseColor("#40000000"));
        }

        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        int width = getWidth();
        if (b.length == 0) {
            return;
        }

        int singleHeight = height / b.length;
        for (int i = 0; i < b.length; i++) {
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight + getPaddingTop();
            paint.setTextSize(26);
            paint.setColor(textColor);
            if (showBkg) {
                paint.setColor(Color.BLACK);
            }
            paint.setAlpha(128);
            paint.setAntiAlias(true);
            if (i == choose) {
                float cSize = paint.measureText(b[i]);
                float xOffest = 0;
                float yOffset = 0;
                if ("I".equals(b[i])) {
                    xOffest = 1;
                    yOffset = -4;
                }
//                canvas.drawCircle(xPos + cSize / 2 + xOffest, yPos - cSize / 2 + yOffset, 20, circlePaint);
                paint.setColor(Color.GREEN);
                paint.setFakeBoldText(true);
            }

            canvas.drawText(b[i], xPos, yPos, paint);

            paint.reset();
        }

    }

    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {

        public void onTouchingLetterEnd();

        public void onTouchingLetterChanged(String s, float y, float x);
    }
}
